var jwt = require("jsonwebtoken");
var mysql = require("mysql");

function mysqlCreate(){
  return mysql.createConnection({
    host: "localhost",
    user: "root",
    password:"root",
    database: "forum",
  })
}

// //token生成
// function createToken(username){
//   /*
//     iss:签发人
//     iat:签发时间回溯30s
//     exp:过期时间 这里可是使用秒数,也可以使用day
//     "{"jti":1,"iss":"gumt.top","user":"goolge","iat":1555650413,"exp":1555657613}"
//     "iat": ~~(Date.now() / 1000)-30,
//     "exp": ~~(Date.now() / 1000)+(60*60),
//   */
// }
function JwtSend(result){
  const user = {
    "jti": 1,
    "iss": "goyang.top",
    "user": result[0].name,
  }
  const token = jwt.sign(user,"goyangforum",{ expiresIn: '1day' });
  const data = {
    username: result[0].name,
    userId: result[0].id,
    token: token
  }
  return data
}

module.exports = {
  JwtSend,
  mysqlCreate
}
